package com.apestech.framework.xml.sql.node;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.xml.pub.core.ElementUtil;
import com.apestech.framework.xml.pub.core.NodeJson;
import com.apestech.framework.xml.sql.modul.QueryhTw;
import org.dom4j.Element;

import java.util.List;

/**
 * @author hup
 * @create 2018-12-10-10:24
 */
public class NodehTw extends ElementUtil{
    private Element node;
    private QueryhTw htw=new QueryhTw();

    public NodehTw(Element node){
        this.node=node;
    }

    public QueryhTw getHtw() {
        return htw;
    }

    public void getAllHtw() throws Exception {
        htw.addBase(getNodeAttr(node).getJSONObject("attrNode"));
        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                Element eml=(Element) list.get(i);
                String nname=eml.getName().toLowerCase();
                switch (nname){
                    case "datafieldhead":{
                        NodeJson nodejson=new NodeJson(eml);
                        nodejson.expain();
                        htw.setDataFieldHead((JSONArray) nodejson.getResult().get("dataFieldHead"));
                        break;
                    }
                    case "pivot":{
                        getPivot(eml);
                        break;
                    }
                }
            }
        }
    }

    private void getPivot(Element e) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                String nname=eml.getName().toLowerCase();
                if("fields".equals(nname)){
                    htw.addPivot(getNodeAttr(eml).getJSONObject("attrNode"));
                }
            }
        }
    }

}
